Skip to content

Add Delete functionality for Peek - #35418

Merged
Jaime Bernardo (jaimecbernardo) merged 21 commits into
microsoft:mainfrom
daverayment:peek-delete
Mar 18, 2025
Merged

Jaime Bernardo (jaimecbernardo) merged 21 commits into
microsoft:mainfrom
daverayment:peek-delete

Conversation

@daverayment

@daverayment Dave Rayment (daverayment) commented Oct 13, 2024 •

Copy link
Copy Markdown
Collaborator

Summary of the Pull Request

This PR adds the ability for users to delete the file currently being previewed in Peek.

PR Checklist

Detailed Description of the Pull Request / Additional comments

There are several new and changed elements in this PR:

  • Navigation has been updated to track forward and backward movement through files. This is necessary so deleting a file always shows the next item in the natural sequence, i.e. the previous item if the user was moving backwards. This replicates Windows Photo and other media viewers.
  • The indexes of deleted items are recorded rather than altering the Items collection (which could be very large). There are now DisplayIndex and DisplayItemCount properties, which reflect the calculated position and total. The old CurrentIndex still points to the actual index of the current item in the collection, but this is now internal only.
  • The MainWindow ViewModel includes quite a bit of navigation-related code. We may wish to split this out into a separate model or provider in the future.
  • When previewing a selection of items (where the number and total are shown in the title bar), these counts will always be shown. Previously, this info would not show if the count was 1. (This was likely because opening Peek while a single item is selected expands the Items collection to cover the entire folder and turns off the "(position/total)" display.)
  • A text message has been added for when there are no more files remaining, i.e. the user has deleted the last one from their selection (or from the current folder if Peek wasn't opened via multi-selection). It may be worthwhile to add a simple icon by the side here - perhaps an image placeholder graphic? - but I've gone with the simple approach for now.
  • Changes were made to the title bar code to ensure that the last filename and "open in application" details were not shown when there are no more files to preview. This was because there was an (entirely reasonable!) assumption in the code that there would always be one or more files being previewed.
  • The delete operation sends the item to the Recycle Bin only. It was considered whether pressing Shift + Delete could do a permanent delete, and this is certainly possible in the future, but this release errs on the side of caution, especially as it's a new feature and involves deleting things.
  • The Delete function itself relies upon a shell file operation, reflecting the fact that we're previewing shell items. This required adding some code to NativeMethods.cs for the API call, struct, flags, and error messages.
  • Only file deletion is allowed. I considered allowing for empty folder deletes, too, but, again, this first release is deliberately conservative.
  • Updated Peek documentation.

Screenshots

No More Files message

Screenshot 2024-10-13 230651 (Small)

File Count For Single Remaining File

image

Validation Steps Performed

(Manual tests only.)

Tested:

  • Single and multi-selection methods of opening Peek.
  • Deleting from beginning, end and middle of a sequence (after opening Peek via multi-selection).
  • Deleting until no files remained.
  • Previewing a folder containing a mix of files and folders.
  • Previewing a folder containing a mix of files which can be previewed and unsupported file types.
  • Attempting to delete folders via Peek.
  • Deleting a file in the selected list via Explorer before it could be previewed.
  • Deleting a file from an external USB drive which doesn't support the Recycle Bin, to confirm that the system confirmation dialog is displayed which warns against permanent file deletion.
  • Cancelling a permanent delete, confirming the file is re-added to the current list.
  • Changing the "Don't show this warning again" setting in the delete confirmation dialog updates the settings.
  • Updating the "Ask for confirmation before deleting files" setting in the Settings app is reflected in Peek immediately, and vice-versa. (The Settings app requires navigating to another page then returning to the Peek settings.)

{
wFunc = FO_DELETE,
pFrom = path + "\0\0",
fFlags = (ushort)(FOF_NOCONFIRMATION | (permanent ? 0 : FOF_ALLOWUNDO)),

@htcfreek Heiko (htcfreek) Oct 14, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this override the recycle bin property? (It is possible to configure the delete confirmation in the recycle bin properties.)

How does Fotos app handle it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replicates how the Photos application handles deletes. Pressing Delete immediately sends the file to the Recycle Bin without popping up a message, and then it moves on to the next file.

The permanent argument is there for possible future use, but is always false for now.

@htcfreek

Copy link
Copy Markdown
Collaborator

Dave Rayment (@daverayment)
Please fix the merge conflicts. (This is the reason for the failing test.)

@github-actions

This comment has been minimized.

@daverayment

Copy link
Copy Markdown
Collaborator Author

Heiko (@htcfreek)

Thanks for your comments and suggestions.

I've updated the code with an error InfoBar, which is shown when a delete fails:

image

I've added user-friendly messages for the most common failures. Others result in a generic message being shown on the InfoBar.

The failure is also logged, with the code and a more technical error message (taken from winerror.h).

If the file still exists after the deletion attempt, it is kept in the items collection for previewing; otherwise it is removed.

An aside: Windows Photo handles files being renamed or deleted differently to Peek, in that it seems to constantly monitor the folder. (Try changing the filename of a photo you're previewing and you can see it update in the UI after a second or two.) We may wish to replicate this functionality in the future, as it would be increase robustness, but that it outside of the scope of this issue.

@crutkas Clint Rutkas (crutkas) added the Needs-Review This Pull Request awaits the review of a maintainer. label Nov 20, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening the pull request.
Generally looks good to me, but there's the issue that this might be unintentionally pressed by the user, getting a file deleted he might not have intended. Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer. I'm OK with a setting to disable this warning, but it should show a confirmation prompt by default.
Hope this makes sense.

@Jay-o-Way

Copy link
Copy Markdown
Collaborator

Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer.

Agree. Could also try to get/use the setting for the Recycle Bin itself.

@htcfreek

Copy link
Copy Markdown
Collaborator

Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer.

Agree. Could also try to get/use the setting for the Recycle Bin itself.

Doesn't the code does this yet? Or better asked: Isn't this handled by Recycle Bin itself?

@Jay-o-Way

Copy link
Copy Markdown
Collaborator

Doesn't the code does this yet? Or better asked: Isn't this handled by Recycle Bin itself?

Nope, the code uses DeleteFile(item) -> FO_DELETE and that's an explicit action. I'd say the setting is an Explorer setting.

@daverayment

Copy link
Copy Markdown
Collaborator Author

Jaime Bernardo (@jaimecbernardo) Heiko (@htcfreek) Jay (@Jay-o-Way)

Thank you for the review and guidance. However, I disagree with displaying a prompt.

The default Windows functionality for file deletions is not to show a confirmation prompt. The whole purpose of the Recycle Bin is to to protect against accidental deletions. The behaviour in Peek was deliberately chosen to be consistent with both Windows Explorer and other file viewer tools such as the Windows Photo App and third party apps like IrfanView. I believe users would find a confirmation dialog unnecessary or redundant every time they want to delete a file, which could be a common action when sorting through a large folder of files. Also, PowerToys is supposed to be for power users, so I don't think we should go out of our way to provide guardrails for easily-reversible common mistakes such as accidentally pressing the delete key (which is also not near the default Peek navigation keys).

To summarise, I don't think we should provide non-default behaviour which would slow down a user's workflow.

@jaimecbernardo

Copy link
Copy Markdown
Contributor

I've tried the Windows "Photos" app, it does show a confirmation prompt:
image

@jaimecbernardo

Copy link
Copy Markdown
Contributor

I imagine you might've already clicked that "Don't show this warning again" Dave Rayment (@daverayment) .
What I'm proposing is having a prompt such as this one from the Windows "Photos" app. But also a setting to allow the prompt to not be shown, but I think the prompt should be shown by default.

@jaimecbernardo

Copy link
Copy Markdown
Contributor

I've also just tried the feature on an external drive. As expected, files were permanently deleted without going to the recycle bin. I don't feel easy causing potential data loss with a feature that wasn't there before without some initial guardrails so the user understands that feature is there 😅 Hope this makes sense to you.

@daverayment

Copy link
Copy Markdown
Collaborator Author

Thanks, Jaime Bernardo (@jaimecbernardo)

Sorry, yes, you're right! I must have clicked the "Don't Show" checkbox when I initially started using Windows Photo. Please forgive the lapse in memory - it was a long time ago... 😬

My main concern was that presenting dialogs every time a delete was requested would get in the way, but I'm fine with your suggestion of replicating the Photo app's behaviour with the "Don't Show" checkbox. This will give me a chance to have a play with the Settings code, too, which I've been avoiding.

I'm very concerned that the delete operation skipped the Recycle Bin for a file on your external device. Would you mind seeing what your Recycle Bin properties are for that drive, please? It is possible to turn off the Recycle Bin on a per-drive basis:

image

The Peek code just calls the underlying Shell API, so it should be consistent with doing a delete from Explorer (without holding Shift down); it is a bug if it behaves differently for external drives.

@Jay-o-Way

Jay (Jay-o-Way) commented Dec 3, 2024 •

Copy link
Copy Markdown
Collaborator

Dave Rayment (@daverayment) using a delete function in most code languages (like DeleteFile(item) -> FO_DELETE) is very explicit - it doesn't care about a recycling bin. Code languages often have a "FileRecycle" function too, but...

Sadly, C# doesn't have a native API to delete files and folders to Recycle Bin. But Visual Basic has.

https://www.c-sharpcorner.com/blogs/extension-methods-for-delete-files-and-folders-to-recycle-bin

Also check out

@jaimecbernardo

Copy link
Copy Markdown
Contributor

I'm very concerned that the delete operation skipped the Recycle Bin for a file on your external device. Would you mind seeing what your Recycle Bin properties are for that drive, please? It is possible to turn off the Recycle Bin on a per-drive basis:

image

Dave Rayment (@daverayment) There's nothing on Recycle Bin for this drive and another I've tested. I'm guessing because their file system is FAT32.

@jaimecbernardo

Copy link
Copy Markdown
Contributor

Here's the last two Settings added to Peek as a reference. Thank you 😉
#26308
#26364

@htcfreek

Copy link
Copy Markdown
Collaborator

Dave Rayment (@daverayment) , Jaime Bernardo (@jaimecbernardo)
For best user behavior the setting should be controllable from the dialogue in Peek (disable message) and the settings page (disable and enable) in PT Settings. Is this possible?

@daverayment

Dave Rayment (daverayment) commented Dec 6, 2024 •

Copy link
Copy Markdown
Collaborator Author

Dave Rayment (@daverayment) using a delete function in most code languages (like DeleteFile(item) -> FO_DELETE) is very explicit - it doesn't care about a recycling bin. Code languages often have a "FileRecycle" function too, but...

Sadly, C# doesn't have a native API to delete files and folders to Recycle Bin. But Visual Basic has.

Hi, Jay (@Jay-o-Way) !

Thanks for the message. It is certainly a shame that System.IO doesn't include recycling options when deleting files.

As you noticed, this update to Peek uses the Shell File Operation FO_DELETE. This is also used internally by Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile() (see the VB Core library source here). FO_DELETE does support a flag to control whether it attempts to move the file to the Recycle Bin (FOF_ALLOWUNDO) and various others to control which progress and error messages are shown.

This isn't enough for our needs, though, so I will add the dialog box to prompt the user when they try to delete a file, as previously discussed. Giving users the option to permanently dismiss this warning replicates what the Windows Photo App does, and is a good compromise for power users.

The reason Jaime Bernardo (@jaimecbernardo) experienced the issue with the FAT32 volume is because I needed to include a flag called FOF_WANTNUKEWARNING, which is very well named! I will correct that in the PR. Thank you for the catch, Jaime!

The VB Core library containing DeleteFile() is 1.2 MB, so I'm keen to just call the Shell API directly to reduce the potential bloat.

@daverayment

Copy link
Copy Markdown
Collaborator Author

Dave Rayment (@daverayment) , Jaime Bernardo (@jaimecbernardo) For best user behavior the setting should be controllable from the dialogue in Peek (disable message) and the settings page (disable and enable) in PT Settings. Is this possible?

Heiko (@htcfreek)

Yes, that's what I assumed we'd be doing. I haven't used the Settings system before, but I'll give it a go and I'll ask if I get stuck!

@htcfreek

Copy link
Copy Markdown
Collaborator

Dave Rayment (@daverayment)
Are you working on the outstanding tasks (delete message with setting, using recycle bin, fat32 bug)?

/needinfo

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jan 14, 2025
@daverayment

Copy link
Copy Markdown
Collaborator Author

Heiko (@htcfreek) Yes, I'm still working on this. Sorry about the delay.

@microsoft-github-policy-service microsoft-github-policy-service Bot added Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams Needs-Team-Response An issue author responded so the team needs to follow up and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jan 14, 2025
@htcfreek Heiko (htcfreek) removed Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams Needs-Team-Response An issue author responded so the team needs to follow up labels Jan 15, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 19 changed files in this pull request and generated 1 comment.

Files not reviewed (13)
  • .github/actions/spell-check/expect.txt: Language not supported
  • src/modules/peek/Peek.FilePreviewer/FilePreview.xaml: Language not supported
  • src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml: Language not supported
  • src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml: Language not supported
  • src/modules/peek/Peek.UI/Strings/en-us/Resources.resw: Language not supported
  • src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml: Language not supported
  • src/settings-ui/Settings.UI/Strings/en-us/Resources.resw: Language not supported
  • src/modules/peek/Peek.UI/Models/NeighboringItems.cs: Evaluated as low risk
  • src/modules/peek/Peek.FilePreviewer/FilePreview.xaml.cs: Evaluated as low risk
  • src/modules/peek/Peek.UI/Services/IUserSettings.cs: Evaluated as low risk
  • src/settings-ui/Settings.UI.Library/PeekProperties.cs: Evaluated as low risk
  • src/modules/peek/Peek.UI/PeekXAML/Views/TitleBar.xaml.cs: Evaluated as low risk
  • src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs: Evaluated as low risk
Comments suppressed due to low confidence (4)

src/modules/peek/Peek.UI/Native/NativeMethods.cs:41

  • The initialization of _deletedItemIndexes should use HashSet() instead of [].
private readonly HashSet<int> _deletedItemIndexes = [];

src/modules/peek/Peek.UI/MainWindowViewModel.cs:204

  • [nitpick] The hwnd parameter should be renamed to windowHandle for clarity.
public void DeleteItem(bool? skipConfirmationChecked, nint hwnd)

src/modules/peek/Peek.UI/MainWindowViewModel.cs:293

  • The ReinstateDeletedFile method does not check if the file still exists before removing it from _deletedItemIndexes, which could lead to inconsistency.
private void ReinstateDeletedFile(IFileSystemItem item, int index)

src/modules/peek/Peek.UI/MainWindowViewModel.cs:271

  • The DeleteFile method should have test coverage to ensure it handles different scenarios correctly (e.g., file in use, access denied).
private int DeleteFile(IFileSystemItem item, nint hwnd)

@drawbyperpetual

Ani (Anirudha Shankar) (drawbyperpetual) commented Feb 28, 2025 •

Copy link
Copy Markdown
Contributor

Any reason not to include an explicit button for this action? The Windows Photos app has one and the other Peek action that one can perform with files (open in default app) also has one. Another reason to include an explicit button is that the Delete key does not seem to work well with text files because of this issue

@daverayment

Copy link
Copy Markdown
Collaborator Author

Any reason not to include an explicit button for this action? The Windows Photos app has one and the other Peek action that one can perform with files (open in default app) also has one. Another reason to include an explicit button is that the Delete key does not seem to work well with text files because of this issue

Thanks for your review, Ani (Anirudha Shankar) (@drawbyperpetual) 😊

The intention was to keep the UI minimal, similar to why there are no icons for basic navigation. I also wasn't keen on having a destructive action button in the title bar. However, I do realise that's personal preference.

With regard to the WebView-related focus, the current behaviour is a good thing with regard to deletes. There is a difference in user expectation between pressing escape (should exit the viewer) and pressing delete while viewing a text file. When viewing text, Peek gives the user a caret and allows them to select text, so it is reasonable to assume that some of them will try to edit the file contents, including pressing delete to remove a paragraph or whatever. These delete keystrokes shouldn't bubble up to the KeyUp handler.

In any case, I'm happy to add the button or wait to see if there is demand for it from the community after the functionality is released. Please let me know.

@drawbyperpetual

Copy link
Copy Markdown
Contributor

Any reason not to include an explicit button for this action? The Windows Photos app has one and the other Peek action that one can perform with files (open in default app) also has one. Another reason to include an explicit button is that the Delete key does not seem to work well with text files because of this issue

Thanks for your review, Ani (Anirudha Shankar) (@drawbyperpetual) 😊

The intention was to keep the UI minimal, similar to why there are no icons for basic navigation. I also wasn't keen on having a destructive action button in the title bar. However, I do realise that's personal preference.

With regard to the WebView-related focus, the current behaviour is a good thing with regard to deletes. There is a difference in user expectation between pressing escape (should exit the viewer) and pressing delete while viewing a text file. When viewing text, Peek gives the user a caret and allows them to select text, so it is reasonable to assume that some of them will try to edit the file contents, including pressing delete to remove a paragraph or whatever. These delete keystrokes shouldn't bubble up to the KeyUp handler.

In any case, I'm happy to add the button or wait to see if there is demand for it from the community after the functionality is released. Please let me know.

Sure; it can always be added later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good and works well in my testing!

@jaimecbernardo

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jaimecbernardo

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jaimecbernardo
Jaime Bernardo (jaimecbernardo) dismissed their stale review March 17, 2025 23:13

PR reviewed by drawbyperpetual user instead

@jaimecbernardo
Jaime Bernardo (jaimecbernardo) merged commit 8e90d8e into microsoft:main Mar 18, 2025
Colin Tso (colin-tso) pushed a commit to colin-tso/PowerToys that referenced this pull request Apr 8, 2025
* Add Delete functionality for Peek.

* Updated the "No More Files" text block to use a Uid to load its resource text. Also altered the text style to be consistent with the FailedFallbackPreviewControl error page.

* Revert "Delete Directory.Packages.props"

This reverts commit 3a10918.

* Attempt to appease the spell-checking bot by renaming flag const.

* Show error message InfoBar if file deletion failed.

* Resolve XAML styling.

* XAML styling fix.

* Settings app updates for new delete confirmation setting.

* Add delete confirmation dialog and settings to Peek. Add shell notification event after delete operation.

* Spelling updates.

* Spelling update.

* Remove permanent delete parameter, YAGNI. Add hwnd parameter to delete so warning dialogs are correctly parented. Fix flags to not hide permanent delete warning.

* Simplify delete confirmation dialog. Remove workaround for focus visual issue. Ensure delete confirmation dialog is closed when the main window visibility is toggled.

* Fix delete delay. Do not regard user cancellations of permanent deletes as an error, but log them as info anyway. More descriptive name for delete confirmation dialog checkbox.

* Fix multiple Content_KeyUp events being raised for MainWindow.

* Synchronise ConfirmFileDelete setting between Peek and Settings app.

* Update following review: split System usings from others; do not log deleted item name.

* Fix XAML style
Sadirano (sadirano) pushed a commit to sadirano/PowerToys that referenced this pull request Jun 10, 2025
* Add Delete functionality for Peek.

* Updated the "No More Files" text block to use a Uid to load its resource text. Also altered the text style to be consistent with the FailedFallbackPreviewControl error page.

* Revert "Delete Directory.Packages.props"

This reverts commit 3a10918.

* Attempt to appease the spell-checking bot by renaming flag const.

* Show error message InfoBar if file deletion failed.

* Resolve XAML styling.

* XAML styling fix.

* Settings app updates for new delete confirmation setting.

* Add delete confirmation dialog and settings to Peek. Add shell notification event after delete operation.

* Spelling updates.

* Spelling update.

* Remove permanent delete parameter, YAGNI. Add hwnd parameter to delete so warning dialogs are correctly parented. Fix flags to not hide permanent delete warning.

* Simplify delete confirmation dialog. Remove workaround for focus visual issue. Ensure delete confirmation dialog is closed when the main window visibility is toggled.

* Fix delete delay. Do not regard user cancellations of permanent deletes as an error, but log them as info anyway. More descriptive name for delete confirmation dialog checkbox.

* Fix multiple Content_KeyUp events being raised for MainWindow.

* Synchronise ConfirmFileDelete setting between Peek and Settings app.

* Update following review: split System usings from others; do not log deleted item name.

* Fix XAML style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs-Review This Pull Request awaits the review of a maintainer.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants